package top.kaoshanji.ch02;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.net.*;
import java.nio.channels.DatagramChannel;
import java.nio.channels.MembershipKey;

/**
 * 代码清单 ch02-07：MulticastChannel示例(2.6.2节)
 * 加入IP地址为 180.90.4.12 的多播组，并对其发送和接收系统状态信息
 * author kaoshanji
 * ^_^
 * create time 2024/12/13 20:19
 */
public class MulticastChannelDemo {

    private static final Logger logger = LogManager.getLogger(MulticastChannelDemo.class);

    public static void main(String[] args) {
        logger.info("...代码清单 ch02-07：MulticastChannel示例(2.6.2节)...");

        try {
            NetworkInterface networkInterface = NetworkInterface.getByName("net1");

            DatagramChannel dc = DatagramChannel.open(StandardProtocolFamily.INET);

            dc.setOption(StandardSocketOptions.SO_REUSEADDR, true);
            dc.bind(new InetSocketAddress(8080));
            dc.setOption(StandardSocketOptions.IP_MULTICAST_IF, networkInterface);

            InetAddress group = InetAddress.getByName("180.90.4.12");
            MembershipKey key = dc.join(group, networkInterface);

        } catch (IOException e) {
            System.out.println(e.getMessage());
        }

    }

}
